﻿using System;
using System.Diagnostics;
using System.Text;
using System.Threading.Tasks;
using EasyNetQ;
using RabbitMQ.Client;
using RabbitMQ.Client.Events;

namespace MdbPhoneBook.Dal
{
    public class HandleMq 
    {
        private IBus m_Bus;

        public HandleMq()
        {
  //          m_Bus = RabbitHutch.CreateBus("host=localhost");
        }

        public void Subscribe()
        {
            //var redisDal = new RedisDal();
            //m_Bus.Subscribe<string>("PhoneBook_Subscribtion_Id", (msg) =>
            //{
            //    Debug.WriteLine("Request received for {0} and will be propagate to Redis", msg);
            //    redisDal.LogStatistic(msg);
            //});

            Task t = new Task(
                () =>
                {
                    var factory = new ConnectionFactory() { HostName = "localhost" };
                    using (var connection = factory.CreateConnection())
                    {
                        using (var channel = connection.CreateModel())
                        {
                            SetupQueue(channel);

                            var basicConsumer = new QueueingBasicConsumer(channel);
                            channel.BasicConsume("MdbPhoneBook", noAck: false, consumer: basicConsumer);

                            Console.WriteLine(" [*] Waiting for messages." +
                                              "To exit press CTRL+C");

                            while (true)
                            {
                                var ea = (BasicDeliverEventArgs)basicConsumer.Queue.Dequeue();

                                var body = ea.Body;
                                var message = Encoding.UTF8.GetString(body);

                                Debug.WriteLine("Request received for {0} and will be propagate to Redis", message);
                                var redisDal = new RedisDal();
                                redisDal.LogStatistic(message);
                                Console.WriteLine(" [x] Received {0}", message);

                                channel.BasicAck(ea.DeliveryTag, false);
                            }
                        }
                    }
                }
                );
            t.Start();
        }

        private static void SetupQueue(IModel channel)
        {
            channel.QueueDeclare("MdbPhoneBook", durable: true, exclusive: false, autoDelete: false, arguments: null);
        }

        public void Publish(string msg = "")
        {
            var factory = new ConnectionFactory() { HostName = "localhost" };
            using (var connection = factory.CreateConnection())
            {
                using (var channel = connection.CreateModel())
                {
                    SetupQueue(channel);

                    var body = Encoding.UTF8.GetBytes(msg);

                    IBasicProperties properties = channel.CreateBasicProperties();
                    properties.SetPersistent(true);
                    
                    channel.BasicPublish(exchange: "", routingKey: "MdbPhoneBook", basicProperties: properties,body: body);
                    Debug.WriteLine(" [x] Sent {0}", msg);
                }
            }
        }

        //public void Dispose()
        //{
        //    m_Bus.Dispose();
        //}
    }
}
